class Solution:
    def sumOfDistancesInTree(self, N: int, edges):
        n=[[0 for i in range(N)] for j in range(N)]
        linklist=[[]for i in range(N)]
        for i in edges:
            n[i[0]][i[1]]=1
            n[i[1]][i[0]]=1
            linklist[i[0]].append(i[1])
            linklist[i[1]].append(i[0])
        for i in range(N):
            length=len(linklist[i])
            for index,k in enumerate(linklist[i]):
                for j in linklist[i][index+1:length]:
                    if n[k][j]==0:
                        n[k][j]=n[i][k]+n[i][j]
                        n[j][k]=n[i][k]+n[i][j]
                        linklist[k].append(j)
                        linklist[j].append(k)
        return [sum(k) for k in n]

s=Solution()
